Ext.define('AM.service.WorkSpace', {
    singleton:true,

    addPage:function (page) {
        ppt.view.workspace.add(page);

        if (ppt.index != -1) {
            var prevPage = ppt.view.workspace.items.get(ppt.index);
            prevPage.hide();
        }
        ppt.index++;
        AM.service.Thumbnails.add(ppt.index);
    },

    go:function (index) {
        if (this._checkIndex(index)) {

            var oldPage = ppt.view.workspace.items.get(ppt.index);
            oldPage.hide();

            var showPage = ppt.view.workspace.items.get(index);
            showPage.show();

            ppt.index = index;
        }
    },


    next:function () {
        if (this._checkIndex(ppt.index + 1)) {
            this.go(ppt.index + 1);
        }
    },


    prev:function () {
        if (this._checkIndex(ppt.index - 1)) {
            this.go(ppt.index - 1);
        }
    },

    deletePage: function() {
        var index = ppt.index;
        var page = ppt.view.workspace.items.get(index);

        if (ppt.index != 0) {
            var showPage = ppt.view.workspace.items.get(index - 1);
            showPage.show();
            ppt.index--;
        }
        else if (ppt.view.workspace.items.length != 1) {
            var showPage = ppt.view.workspace.items.get(index + 1)
            showPage.show();
            ppt.index++;
        }
        Ext.destroy(page);
        delete page;
        AM.service.Thumbnails.delete(index);
    },

    _checkIndex:function (index) {
        if (index < 0) {
            alert('index < 0')
        }
        else if (index > ppt.view.workspace.items.length - 1) {
            alert('index > max')
        }
        else {
            return true
        }

    }
});